package com.hd.pzhwlxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.pzhwlxt.pojo.BuildInfo;
import com.hd.pzhwlxt.service.BuildInfoService;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/build/info")
public class BuildInfoController {
    @Autowired
    private BuildInfoService buildInfoService;

    @GetMapping("/getAll")
    public Response<Page<BuildInfo>> getAllBuildInfo(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "15") Integer pageSize){
        Page<BuildInfo> build = buildInfoService.getAllBuildInfo(pageNum,pageSize);
        if(build!=null){
            return Response.createSuccessResponse("查询成功", build);
        }
        return Response.createErrorResponse("查询失败");
    }

    @GetMapping("/get/{id}")
    public Response<BuildInfo> getBuildById(@PathVariable Integer id){
        BuildInfo build = buildInfoService.getById(id);
        if(build!=null){
            return Response.createSuccessResponse("查询成功",build);
        }
        return Response.createErrorResponse("查询失败");
    }

    @PostMapping("/add")
    public Response<String> addBuildInfo(@RequestBody BuildInfo buildInfo){
        System.out.println(buildInfo);
        boolean save = buildInfoService.save(buildInfo);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @DeleteMapping("/delmore/{ids}")
    public Response<String> deleteBuildInfoByIds(@PathVariable Integer[] ids){
        boolean remove = buildInfoService.removeByIds(Arrays.asList(ids));
        if (remove){
            return Response.createSuccessResponse("删除成功");
        }
        return Response.createErrorResponse("删除失败");
    }

    @PutMapping("/update")
    public Response<String> updateBuildInfo(@RequestBody BuildInfo buildInfo){
        boolean update = buildInfoService.updateById(buildInfo);
        if (update){
            return Response.createSuccessResponse("修改成功");
        }
        return Response.createErrorResponse("修改失败");
    }
}
